package ntu.sce.fyp.easilocation;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ntu.sce.fyp.easilocation.datastore.DatastoreHelper;

@SuppressWarnings("serial")
public class SignInServlet extends HttpServlet {
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		/*UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            resp.setContentType("text/plain");
            resp.getWriter().println("Hello, " + user.getNickname());
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }*/
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/xml");
		PrintWriter pw = resp.getWriter();
		
		/** retrieve username and password */
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		/** check for correct username and password */
		boolean flag = DatastoreHelper.INSTANCE.checkUser(username, password);
		
		if (flag) {
			pw.println("<login>");
			pw.println("	<status>SUCCESS</status>");
			pw.println("	<message>Log in successfully</message>");
			pw.println("</login>");
		} else {
			pw.println("<login>");
			pw.println("	<status>ERROR</status>");
			pw.println("	<message>Not an existing user</message>");
			pw.println("</login>");
		}
		pw.close();
	}
}
